﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Utility;
using Domain.Entities;
using Microsoft.AspNetCore.Http;
using JFnetcore.Models;
using Application.UserApp;
using Application.RoleApp;
using Application.LogApp;
using static Utility.EnumUtil;

namespace JFnetcore.Controllers
{
    public class HomeController : BaseController
    {

        private IUserAppService _userAppService;
        private IRoleAppService _roleAppService;
        private ILogAppService _logAppService;
        public HomeController(IUserAppService userAppService, IRoleAppService roleAppService, ILogAppService logAppService)
        {
            _userAppService = userAppService;
            _roleAppService = roleAppService;
            _logAppService = logAppService;
        }


        #region 主页
        /// <summary>
        /// 打开主页视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            //获取Session信息并且返回前台
            SessionModel userSession = getUserSession();

            ViewBag.User_name = userSession.User.User_name;
            ViewBag.Login_name = userSession.User.Login_name;
            ViewBag.Rootmenu = _roleAppService.GetRoleRootMenu(userSession.User.Role_id);
            return View();
        }

        /// <summary>
        /// 加载菜单
        /// </summary>
        /// <param name="id">根目录菜单id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult homeMenu(string id)
        {
            return Content(_roleAppService.GetRoleMenuHtml(getUserSession().User.Role_id, Int32.Parse(id)));
        }

        /// <summary>
        ///退出登录 
        /// </summary>
        /// <returns></returns>
        [HttpGet("Layout")]
        public IActionResult Layout()
        {
            SessionModel sm = getUserSession();
            _logAppService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)OperationEnum.Logout, "");
            HttpContext.Session.SetString("CurrentUser", "");
            return Redirect("/Login/Index/out");
        }

        #endregion


        public IActionResult empty()
        {
            return View();
        }
    }
}
